﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Web.Support;
using System.Web.Security;

namespace Web.Support
{
    public class BaseMasterPage : MasterPage,IUserProfile
    {
        public BaseMasterPage()
        {
            this.Init += new EventHandler(BaseMasterPage_Init);
        }

        protected void BaseMasterPage_Init(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "no-cache");
            Response.Expires = -1;

            //MenuID = (Request.Params["menuid"] == null ? 0 : Convert.ToInt32(Request.Params["menuid"]));

            UserLoggedIn = "Anonymous";
            //SubSectorName = string.Empty;

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = this.Context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData.Trim();
                        if (!string.IsNullOrEmpty(userData))
                        {
                            string[] datas = userData.Split('|');

                            UserID = int.Parse(datas[0]);

                            if (!string.IsNullOrEmpty(datas[1]))
                                GroupID = int.Parse(datas[1]);

                            if (!string.IsNullOrEmpty(datas[2]))
                                GroupAppID = (datas[2].ToString());

                            if (!string.IsNullOrEmpty(datas[3]))
                                UserCode = datas[3].ToString();

                            
                        }
                    }
                }
            }

        }

        public string UserLoggedIn { get; set; }
        public string UserCode { get; set; }
        public int UserID { get; set; }
        public int GroupID { get; set; }
        public string GroupAppID { get; set; }

    }
}